import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:lazy_food/components/keepAliveWrapper/keep_alive_arapper.dart';
import 'package:lazy_food/models/recipe_model.dart';
import 'package:lazy_food/models/story_model.dart';
import 'package:lazy_food/models/tab_model.dart';
import 'package:lazy_food/models/vip_model.dart';
import 'package:lazy_food/net/network.dart';
import 'package:lazy_food/util/string+.dart';
import '../../components/group/group.dart';

class VipPage extends StatefulWidget {
  TabModel tab;
  VipPage({Key? key, required this.tab}) : super(key: key);
  @override
  State<VipPage> createState() => _VipPageState();
}

class _VipPageState extends State<VipPage> {
  EasyRefreshController _refreshController =
      EasyRefreshController(controlFinishRefresh: true);
  List<VipModel> _models = [];

  void _onRefresh() {
    EasyLoading.show();
    Network.getHomeVip().then((value) {
      setState(() {
        _models = value;
      });
      _refreshController.finishRefresh();
      EasyLoading.dismiss();
    });
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh.builder(
      controller: _refreshController,
      refreshOnStart: true,
      onRefresh: _onRefresh,
      childBuilder: (context, physics) {
        double imgW = MediaQuery.of(context).size.width;
        double imgH = imgW * 0.75;
        return KeepAliveWrapper(
          child: ListView.builder(
            physics: physics,
            itemBuilder: (context, index) {
              if (index == 0) {
                return Stack(
                  children: [
                    Image.asset(
                      'assets/images/prime_tab_banner.png',
                      width: imgW,
                      height: imgH,
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        SizedBox(height: 100),
                        Container(
                          height: 20,
                          child: Text(
                            '懒人会员',
                            style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                        SizedBox(height: 15),
                        Container(
                          padding: EdgeInsets.symmetric(horizontal: 20),
                          child: Text(
                            '1500+精选会员专属菜谱、500+健身减脂菜谱，只需一次订阅',
                            style: TextStyle(fontSize: 14),
                            softWrap: true,
                          ),
                        ),
                        SizedBox(height: 15),
                        TextButton(
                          onPressed: () {},
                          clipBehavior: Clip.hardEdge,
                          child: Text(
                            '新用户首月优惠￥5',
                            style: TextStyle(color: Colors.white),
                          ),
                          style: ButtonStyle(
                            backgroundColor:
                                MaterialStatePropertyAll(Colors.blue),
                          ),
                        ),
                      ],
                    ),
                  ],
                );
              } else {
                VipModel model = _models[index - 1];
                List<RecipeModel>? recipes = model.data.recipes;
                List<StoryModel>? stories = model.data.stories;
                if (recipes != null) {
                  return GroupView(
                    list: recipes.map(
                      (e) {
                        return GroupModel(
                            e.image.url_pattern.toImage(), e.name);
                      },
                    ).toList(),
                    title: model.data.title,
                  );
                } else if (stories != null) {
                  return GroupView(
                    list: stories.map(
                      (e) {
                        return GroupModel(
                            e.cover_image.url_pattern.toImage(), e.name);
                      },
                    ).toList(),
                    title: model.data.title,
                  );
                } else {
                  return Container();
                }
              }
            },
            itemCount: _models.length + 1,
          ),
        );
      },
    );
  }
}
